package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import server.business.mapper.TransfusionReviewMapper;
import server.business.pojo.TransfusionAutologous;
import server.business.pojo.TransfusionReview;
import server.business.pojo.TransfusionSurgey;
import server.business.service.TransfusionAutologousService;
import server.business.mapper.TransfusionAutologousMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Galaxy
 * @description 针对表【transfusion_autologous】的数据库操作Service实现
 * @createDate 2022-11-08 12:46:24
 */
@Service("transfusionAutologousService")
public class TransfusionAutologousServiceImpl extends ServiceImpl<TransfusionAutologousMapper, TransfusionAutologous>
        implements TransfusionAutologousService {
    @Autowired
    private TransfusionAutologousMapper transfusionAutologousMapper;
    @Autowired
    private TransfusionReviewMapper transfusionReviewMapper;

    /**
     * 申请自体输血
     * 
     * @param transfusionAutologous
     * @return
     */
    @Override
    public int tranAutoAdd(TransfusionAutologous transfusionAutologous) {
        int tranAutoCreate = transfusionAutologousMapper.insert(transfusionAutologous);
        return tranAutoCreate;
    }

    /**
     * 展示自体输血申请审核信息
     * 
     * @param transfusionNumber
     * @return
     */
    @Override
    public ArrayList<Object> getAutoInfo(String transfusionNumber) {
        return transfusionAutologousMapper.showAuto(transfusionNumber);
    }

    /**
     * 批量逻辑删除（删除review表里的同时也删除auto表）
     * 
     * @param ids
     * @return
     */
    @Override
    public ArrayList<Long> selectId(ArrayList<Long> ids) {

        List<String> list = new ArrayList<>();
        for (Long id : ids) {
            QueryWrapper<TransfusionReview> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TransfusionReview::getId, id);
            TransfusionReview t = transfusionReviewMapper.selectOne(queryWrapper);
            System.out.println(t.getTransfusionNumber());
            list.add(t.getTransfusionNumber());
            System.out.println(list.size());
        }

        ArrayList<Long> list1 = new ArrayList<>();
        for (String transfusionNumber : list) {
            QueryWrapper<TransfusionAutologous> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(TransfusionAutologous::getTransfusionNumber, transfusionNumber);
            TransfusionAutologous t1 = transfusionAutologousMapper.selectOne(queryWrapper1);
            System.out.println(t1.getId());
            list1.add(t1.getId());
        }
        return list1;
    }

    // *************************************lrz*************************start
    @Override
    @SuppressWarnings("")
    public IPage<TransfusionAutologous> getPage1(int currentPage, int pageSize,
            TransfusionAutologous transfusionAutologous) {
        LambdaQueryWrapper<TransfusionAutologous> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Strings.isNotBlank(transfusionAutologous.getTransfusionNumber()),
                        TransfusionAutologous::getTransfusionNumber,
                        transfusionAutologous.getTransfusionNumber())
                .eq(Strings.isNotBlank(transfusionAutologous.getTransfusionType()),
                        TransfusionAutologous::getTransfusionType,
                        transfusionAutologous.getTransfusionType())
                .eq(Strings.isNotBlank(transfusionAutologous.getTransfusionVolume()),
                        TransfusionAutologous::getTransfusionVolume,
                        transfusionAutologous.getTransfusionVolume())
                .eq(Strings.isNotBlank(transfusionAutologous.getPatientIdcard()),
                        TransfusionAutologous::getPatientIdcard,
                        transfusionAutologous.getPatientIdcard());
        IPage<TransfusionAutologous> page = new Page<>(currentPage, pageSize);
        transfusionAutologousMapper.selectPage(page, queryWrapper);
        return page;
    }
    // *************************************lrz*************************end

}
